package flc.ast.widget.time;

import android.annotation.SuppressLint;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.graphics.Color;
import android.widget.RemoteViews;

import com.blankj.utilcode.util.TimeUtils;

import java.text.SimpleDateFormat;

import flc.ast.R;
import flc.ast.utils.AppSpUtils;
import stark.common.basic.utils.BitmapUtil;

public class Time2Widget extends BaseTimeWidget {

    public Time2Widget(int appWidgetId) {
        super(appWidgetId);
    }

    @Override
    protected void onUpdate(Context context) {
        @SuppressLint("RemoteViewLayout") RemoteViews remoteViews =
                new RemoteViews(context.getPackageName(), R.layout.widget_time2);
        //准备数据
        String date = TimeUtils.getNowString(new SimpleDateFormat("MM月dd日"));
        String hour = TimeUtils.getNowString(new SimpleDateFormat("HH"));
        String minute = TimeUtils.getNowString(new SimpleDateFormat("mm"));
        String second = TimeUtils.getNowString(new SimpleDateFormat("ss"));
        //更新显示数据
        String time2Color = AppSpUtils.getTime2Color();
        remoteViews.setTextViewText(R.id.tvTime2Date, date);
        remoteViews.setImageViewBitmap(R.id.tvTime2TimeH, BitmapUtil.text2BmpWithAssetFont(context, hour,
                Color.parseColor(time2Color), 32, "font/ziti4.otf"));
        remoteViews.setImageViewBitmap(R.id.tvTime2TimeM, BitmapUtil.text2BmpWithAssetFont(context, minute,
                Color.parseColor(time2Color), 32, "font/ziti4.otf"));
        remoteViews.setImageViewBitmap(R.id.tvTime2TimeS, BitmapUtil.text2BmpWithAssetFont(context, second,
                Color.parseColor(time2Color), 32, "font/ziti4.otf"));
        remoteViews.setImageViewBitmap(R.id.tvTime2Dot1, BitmapUtil.text2BmpWithAssetFont(context, ":",
                Color.parseColor(time2Color), 32, "font/ziti4.otf"));
        remoteViews.setImageViewBitmap(R.id.tvTime2Dot2, BitmapUtil.text2BmpWithAssetFont(context, ":",
                Color.parseColor(time2Color), 32, "font/ziti4.otf"));
        remoteViews.setTextColor(R.id.tvTime2Week1, Color.parseColor("#50FFFFFF"));
        remoteViews.setTextColor(R.id.tvTime2Week2, Color.parseColor("#50FFFFFF"));
        remoteViews.setTextColor(R.id.tvTime2Week3, Color.parseColor("#50FFFFFF"));
        remoteViews.setTextColor(R.id.tvTime2Week4, Color.parseColor("#50FFFFFF"));
        remoteViews.setTextColor(R.id.tvTime2Week5, Color.parseColor("#50FFFFFF"));
        remoteViews.setTextColor(R.id.tvTime2Week6, Color.parseColor("#50FFFFFF"));
        remoteViews.setTextColor(R.id.tvTime2Week7, Color.parseColor("#50FFFFFF"));
        remoteViews.setTextColor(getWeekView(), Color.WHITE);
        remoteViews.setTextColor(R.id.tvTime2Hour, Color.parseColor(time2Color));
        remoteViews.setTextColor(R.id.tvTime2Min, Color.parseColor(time2Color));
        remoteViews.setTextColor(R.id.tvTime2Sec, Color.parseColor(time2Color));

        remoteViews.setInt(R.id.llTime2Bg, "setBackgroundColor", AppSpUtils.getTime2Bg());

        AppWidgetManager.getInstance(context).updateAppWidget(appWidgetId, remoteViews);
    }

    //获取需要变色的控件id
    private int getWeekView() {
        int weekId = R.id.tvTime2Week1;
        String week = TimeUtils.getChineseWeek(TimeUtils.getNowString());
        switch (week) {
            case "周一":
                weekId = R.id.tvTime2Week1;
                break;
            case "周二":
                weekId = R.id.tvTime2Week2;
                break;
            case "周三":
                weekId = R.id.tvTime2Week3;
                break;
            case "周四":
                weekId = R.id.tvTime2Week4;
                break;
            case "周五":
                weekId = R.id.tvTime2Week5;
                break;
            case "周六":
                weekId = R.id.tvTime2Week6;
                break;
            case "周日":
                weekId = R.id.tvTime2Week7;
                break;
        }
        return weekId;
    }
}